package com.whut.monitor.dao.impl;

import com.whut.monitor.dao.IBaseDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Repository
public class BaseDaoImpl<T> implements IBaseDao<T> {

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * 获得当前事物的session
     * @return
     */
    public Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    @Override
    public void save(T entity) {
        if (entity != null) {
            this.getCurrentSession().save(entity);
        }
    }

    @Override
    public void  bathSave(List<T> list) {
        Session session=this.getCurrentSession();
        if (list != null) {
            for(int i=1;i<=list.size();i++){
                session.save(list.get(i-1));
                if(i% 10==0){
                    session.flush();
                    session.clear();
                }
            }
        }
    }

    @Override
    public T findOne(Class<T> c, Serializable id) {
        return (T) this.getCurrentSession().get(c, id);
    }

    @Override
    public T findOne(String hql) {
        return findOne(hql,null);
    }

    @Override
    public T findOne(String hql, Map<String, Object> params) {
        Query query = this.getCurrentSession().createQuery(hql);
        if ((params != null) && !params.isEmpty()) {
            for (String key : params.keySet()) {
                query.setParameter(key, params.get(key));
            }
        }
        return (T) query.setMaxResults(1).uniqueResult();
    }

    @Override
    public List<T> findList(String hql) {
        return findList(hql,null);
    }

    @Override
    public List<T> findList(String hql, Map<String, Object> params) {
        Query q = this.getCurrentSession().createQuery(hql);
        if ((params != null) && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        return q.list();
    }

    @Override
    public List<T> findList(String hql, Map<String, Object> params, int page, int rows) {
        Query q = this.getCurrentSession().createQuery(hql);
        if ((params != null) && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
    }

    @Override
    public List<T> findList(String hql, int page, int rows) {
        Query q = this.getCurrentSession().createQuery(hql);
        return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
    }

    @Override
    public void delete(T entity) {
        if (entity != null) {
            this.getCurrentSession().delete(entity);
        }
    }

    @Override
    public void update(T entity) {
        if (entity != null) {
            this.getCurrentSession().update(entity);
        }
    }

    @Override
    public void saveOrUpdate(T o) {
        if (o != null) {
            this.getCurrentSession().saveOrUpdate(o);
        }
    }

    @Override
    public Long count(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        return (Long) q.uniqueResult();
    }

    @Override
    public Long count(String hql, Map<String, Object> params) {
        Query q = this.getCurrentSession().createQuery(hql);
        if ((params != null) && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        return (Long) q.uniqueResult();
    }

    @Override
    public int executeHql(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        return q.executeUpdate();
    }

    @Override
    public int executeHql(String hql, Map<String, Object> params) {
        Query q = this.getCurrentSession().createQuery(hql);
        if ((params != null) && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        return q.executeUpdate();
    }

}